package ytdp.careagle.com.ytdp.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import ytdp.careagle.com.ytdp.R;

/**
 * Created by admin on 2017/9/21.
 */

public class MySectorSelectView extends View {

    private Paint paint;
    private Path pathOut;
    private float scale = 0.8f;
    private float scaleSelect = 0.7f;
    private Path pathInner;
    private Path pathOpRight;
    private Path pathOpCenter;
    private Path pathOpLeft;
    private Region regionRight;
    private Region regionLeft;
    private Region regionCenter;
    private int mCenterCircleX;
    private int mCenterCircleY;
    private int state = 1;//0 left   1 center   2 right
    private Paint paintSelected;
    private Path pathSelected;
    private StateChangeListener listener;
    private TextPaint paintText;
    private Path pathText;
    private String text = "车况良好";
    private int widthText;
    private int heightText;
    private float tran;
    private int textColor;

    public MySectorSelectView(Context context) {
        this(context, null);
    }

    public MySectorSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paintText = new TextPaint();
        paintText.setTextSize(getResources().getDimension(R.dimen.sp12));
        paintText.setAntiAlias(true);
        textColor = getResources().getColor(R.color.text_title);
        paintText.setColor(textColor);
        //TODO 从中间开始绘制
        paintText.setTextAlign(Paint.Align.CENTER);

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(getResources().getDimension(R.dimen.dp1));
//        paint.setStyle(Paint.Style.STROKE);//空心
        paint.setColor(getResources().getColor(R.color.guestPaint_color_wrong_30));

        paintSelected = new Paint();
        paintSelected.setAntiAlias(true);
        paintSelected.setColor(Color.RED);
        pathOut = new Path();
        pathInner = new Path();
        pathSelected = new Path();
        pathOpRight = new Path();
        pathOpCenter = new Path();
        pathOpLeft = new Path();
        pathText = new Path();
        regionLeft = new Region();
        regionCenter = new Region();
        regionRight = new Region();

        Rect rect = new Rect();
        paintText.getTextBounds(text, 0, text.length(), rect);
        widthText = rect.width();
        heightText = rect.height();
        tran = getResources().getDimension(R.dimen.dp1);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        pathOut.reset();
        pathInner.reset();
        //将画布的坐标原点移到圆心位置
        canvas.translate((width + getPaddingLeft() - getPaddingRight()) / 2,
                (height + getPaddingTop() - getPaddingBottom()) / 2);
        float radius = Math.min(width, height) / 2;
        float innerRadius = radius * scale;
        float textRadius = radius * 0.9f;
        RectF rect = new RectF(-radius, -radius, radius, radius);
        RectF rectInner = new RectF(-innerRadius, -innerRadius, innerRadius, innerRadius);
        RectF rectText = new RectF(-textRadius, -textRadius, textRadius, textRadius);
        addArc(pathOut, radius, rect, 0, 60);
        addArc(pathInner, innerRadius, rectInner, 0, 60);
        pathOpRight.op(pathOut, pathInner, Path.Op.DIFFERENCE);


        pathOut.reset();
        pathInner.reset();
        addArc(pathOut, radius, rect, 60, 60);
        addArc(pathInner, innerRadius, rectInner, 60, 60);
        pathOpCenter.op(pathOut, pathInner, Path.Op.DIFFERENCE);

        pathOut.reset();
        pathInner.reset();
        addArc(pathOut, radius, rect, 120, 60);
        addArc(pathInner, innerRadius, rectInner, 120, 60);
        pathOpLeft.op(pathOut, pathInner, Path.Op.DIFFERENCE);
        pathOut.reset();
        pathInner.reset();

//        paint.setColor(Color.GREEN);
        canvas.drawPath(pathOpRight, paint);
//        paint.setColor(Color.GRAY);
        canvas.drawPath(pathOpCenter, paint);
//        paint.setColor(Color.BLUE);
        canvas.drawPath(pathOpLeft, paint);
        mCenterCircleX = (width + getPaddingLeft() - getPaddingRight()) / 2;
        mCenterCircleY = (height + getPaddingTop() - getPaddingBottom()) / 2;

        float selectedRadius = radius * scaleSelect;
        RectF rectSelected = new RectF(-selectedRadius, -selectedRadius, selectedRadius, selectedRadius);
        switch (state) {
            case 0://left
                addArc(pathOut, radius, rect, 120, 60);
                addArc(pathInner, selectedRadius, rectSelected, 120, 60);
                break;
            case 1://center
                addArc(pathOut, radius, rect, 60, 60);
                addArc(pathInner, selectedRadius, rectSelected, 60, 60);
                break;
            case 2://right
                addArc(pathOut, radius, rect, 0, 60);
                addArc(pathInner, selectedRadius, rectSelected, 0, 60);
                break;
        }
        if (state != -1) {
            pathSelected.op(pathOut, pathInner, Path.Op.DIFFERENCE);
            canvas.drawPath(pathSelected, paintSelected);
        }

        if (state == 2) {
            paintText.setColor(Color.WHITE);
        } else {
            paintText.setColor(textColor);
        }
        drawText(canvas, rectText, 0, 60, "车况优秀");
        if (state == 1) {
            paintText.setColor(Color.WHITE);
        } else {
            paintText.setColor(textColor);
        }
        drawText(canvas, rectText, 60, 60, "车况良好");
        if (state == 0) {
            paintText.setColor(Color.WHITE);
        } else {
            paintText.setColor(textColor);
        }
        drawText(canvas, rectText, 120, 60, "车况一般");

        pathOut.reset();
        pathInner.reset();
    }

    private void drawText(Canvas canvas, RectF rectText, int startAngle, int angleLength, String text) {
        pathText.reset();
        //TODO 因为要逆时针写文字  所以角度应该逆时针  我真他妈的聪明啊
        pathText.addArc(rectText, startAngle + angleLength, -angleLength);
//        PathMeasure pathMeasure = new PathMeasure(pathText, false);
        canvas.drawTextOnPath(text, pathText, 0, heightText / 2 - tran, paintText);
    }

    private void addArc(Path path, float radius, RectF rect, float startAngle, float angleLength) {
        path.moveTo(0, 0);
        path.lineTo(radius, 0);
        path.addArc(rect, startAngle, angleLength);
        path.lineTo(0, 0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                int x = (int) event.getX() - mCenterCircleX;//因为绘图的时候移动到中间了  所以偏移相应的位置
                int y = (int) event.getY() - mCenterCircleY;
                RectF bounds = new RectF();
                pathOpLeft.computeBounds(bounds, true);
                regionLeft.setPath(pathOpLeft, new Region((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom));

                pathOpCenter.computeBounds(bounds, true);
                regionCenter.setPath(pathOpCenter, new Region((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom));

                pathOpRight.computeBounds(bounds, true);
                regionRight.setPath(pathOpRight, new Region((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom));

                if (regionLeft.contains(x, y)) {
                    if (state == 0) {
                        return true;
                    }
                    state = 0;
                } else if (regionCenter.contains(x, y)) {
                    if (state == 1) {
                        return true;
                    }
                    state = 1;
                } else if (regionRight.contains(x, y)) {
                    if (state == 2) {
                        return true;
                    }
                    state = 2;
                } else {
                    return true;
                }
                if (listener != null) {
                    listener.stateChange(state);
                }
                postInvalidate();
                break;

        }
        return true;
    }

    public interface StateChangeListener {
        void stateChange(int state);
    }

    public void setListener(StateChangeListener listener) {
        this.listener = listener;
    }
}
/*
画点
canvas.drawPosText(str,new float[]{
      100,100,    // 第一个字符位置
      200,200,    // 第二个字符位置
      300,300,    // ...
      400,400,
      500,500
},textPaint);
* */